package org.languageguide.user;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.languageguide.storage.DBConnector;
import org.languageguide.versioncontrol.Editable;


public class User{

	private String name;
	private String email;
	private int id;
	private String language;
	private String username;
	private String editingLanguage;
	private String password;
	private String website;
	//private String hashedPw;
	public final static String sessionId = "User";
	//private HttpSession session = null;
	private Editable editable;
	
	public User(ResultSet rs) throws SQLException{
		name = rs.getString("fullname");
		email = rs.getString("email");
		//password = rs.getString("password");
		username = rs.getString("username");
		id = rs.getInt("userid");
		System.out.println("email" + email);
		editingLanguage = rs.getString("editing_language");
		language = rs.getString("language");
		website = rs.getString("website");
		
		System.out.println("language" + language);
		
		//this.session = session;
		if (language == null){
			language ="en";
		}
	}
	
	public User(String name, String username, String email, String password, String lang){
		this.name = name;
		this.username = username;
		this.email = email;
		this.password = password;
		this.editingLanguage = lang;
	}
	
    public String getHashedPw(){
    	//System.out.println("password" + password + " " + PasswordUtility.getHash(password));
    	return PasswordUtility.getHash(password);
    }
	
	public boolean isActive(){
		//if (session == null){
			//return
		//}
		return true;
	}

	public void store(){
		//password, , ?
		String sql = "INSERT INTO Users2(email, code, hashed_password, editing_language, fullname, username) VALUES (?, ?, ?, ?, ?, ?)";
		DBConnector connector = DBConnector.getInstance();
		Connection connection = connector.getConnection();
		String hash = PasswordUtility.getHash(password);
		try {
			System.out.println(" "  + email + " " + hash +  " " + username + " " + editingLanguage + " " + name);
			PreparedStatement statement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
			statement.setString(1, email);
			//statement.setString(2, password);
			String code = PasswordUtility.getHash(username);
			statement.setString(2, code);
			statement.setString(3, hash);
			statement.setString(4, editingLanguage);
			statement.setString(5, name);
			statement.setString(6, username);
			statement.executeUpdate();
			ResultSet keys = statement.getGeneratedKeys();
			keys.next();
			id = keys.getInt(1);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		finally{
			connector.close(connection);
		}
	}
	//
	
	public boolean isAdmin(){
		if (username != null && username.equals("tblackmon")){
			return true;
		}
		return false;
	}
	
	public void updateIncludingUsername(){
		String sql = "UPDATE Users2 SET email = ?, fullname = ?, website = ?, username = ?  where userID = ?";
		DBConnector connector = DBConnector.getInstance();
		Connection connection = connector.getConnection();
		try {
			//System.out.println(" "  + email + " " + password +  " " + username + " " + editingLanguage + " " + name);
			PreparedStatement statement = connection.prepareStatement(sql);
			statement.setString(1, email);
			statement.setString(2, name);
			statement.setString(3, website);
			statement.setString(4, username);
			statement.setInt(5, id);
			statement.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		finally{
			connector.close(connection);
		}			
	}
	
	public void update(){
		String sql = "UPDATE Users2 SET email = ?, fullname = ?, website = ? where userID = ?";
		DBConnector connector = DBConnector.getInstance();
		Connection connection = connector.getConnection();
		//String hash = PasswordUtility.getHash(password);
		try {
			//System.out.println(" "  + email + " " + password +  " " + username + " " + editingLanguage + " " + name);
			PreparedStatement statement = connection.prepareStatement(sql);
			statement.setString(1, email);
			statement.setString(2, name);
			statement.setString(3, website);
			statement.setInt(4, id);
			statement.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		finally{
			connector.close(connection);
		}	
	}

	public String getFullName(){
		return name; 
	}
	
	public String getEmail(){
		return email; 
	}
	
	public String getUserName(){
		return username; 
	}
	
	public void setUserName(String username){
		this.username = username; 
	}	
	
	public void setFullname(String name){
		this.name = name; 
	}
	
	public void setEmail(String email){
		this.email = email; 
	}

	public String getEditingLanguage(){
		return editingLanguage; 
	}
	
	public void setEditingLanguage(String language){
		editingLanguage = language; 
	}	

	public String getLanguage(){
		return language;
	}

	public int getId(){
		return id;
	}
	
	public Integer getIdInteger(){
		return new Integer(id);
	}
	
	public void setLock(Editable editable){
		this.editable = editable;
	}
	public boolean hasLock(){
		return (editable != null);
	}
	
	public String getWebsite(){
		return website;
	}
	
	public void setWebsite(String website){
		this.website = website;
	}
	
	public boolean hasOtherLock(Editable categoryToCheck){
		if(editable == null)
			return false;
		if (editable.getKey().equals(categoryToCheck.getKey()))
			return false;
		return true;
	}
	
	public void unlockCategories(){
		if (editable != null){
			System.out.println("category unlocked" + editable.getKey());
			editable.finishEdit();
			editable = null;
		}
	}
}